
// panel object
local PANEL = { };


/*------------------------------------
	initialize the playlist
------------------------------------*/
function PANEL:Init( )

	// create list
	self.List = vgui.Create( "MXListView", self );
	
	// create list column
	self.ListFilename = self.List:AddColumn( "#musex_playlist" );
	
	// setup the list double-click function
	self.List.DoDoubleClick = function( self, lineid, line )
	
		// get the song
		local song = self.GetParent( self ):GetSelectedPlaylistSong();
		
		// make sure we got one
		if ( song ) then
		
			// load the song
			MuseX.LoadSong( song );
			
			// play the song
			MuseX.Play();
			
		end
		
	end
	
	// setup the OnSorted event
	self.List.OnSorted = function() MuseX.UpdateNowPlaying(); end
	
	// create the "add song(s)" button
	self.AddButton = vgui.Create( "DImageButton", self );
	// tooltip
	self.AddButton:SetTooltip( "#musex_playlist_addsong" );
	// give it a default image
	self.AddButton:SetImage( "brick/brick_model" );
	// hook the image for the skin to change
	MuseX.SkinHook( "PlaylistAdd", self.AddButton, "SetImage" );
	// setup "add song(s)" click command
	self.AddButton.DoClick = function( self ) self:GetParent():OpenFileDialog(); end;
	
	// create the "remove song(s)" button
	self.RemoveButton = vgui.Create( "DImageButton", self );
	// tooltip
	self.RemoveButton:SetTooltip( "#musex_playlist_removesong" );
	// set the default image
	self.RemoveButton:SetImage( "brick/brick_model" );
	// hook the image for the skin to change
	MuseX.SkinHook( "PlaylistRemove", self.RemoveButton, "SetImage" );
	// setup the "remove song(s)" button
	self.RemoveButton.DoClick = function( self )
	
		// get the selected song items
		local selected = self.GetParent( self ).List:GetSelected( self.GetParent( self ).List );
		
		// create empty song list
		local songlist = {};
		
		// loop through each item
		for _, s in pairs( selected ) do
		
			// save the filename to the song list
			songlist[ #songlist + 1 ] = s.Filename;
		
		end
		
		// remove from playlist
		MuseX.RemoveFromPlaylist( songlist );
		
	end
	
	// create the manage button
	self.ManageButton = vgui.Create( "MXButton", self );
	// tooltip
	self.ManageButton:SetTooltip( "#musex_playlist_manage" );
	// tooltip
	self.ManageButton:SetText( "..." );
	// setup the manage command
	self.ManageButton.DoClick = function( self ) self:GetParent():OpenManageDialog(); end;

end


/*------------------------------------
	layout the playlist
------------------------------------*/
function PANEL:PerformLayout( )

	// let the BaseClass do its shit
	self.BaseClass.PerformLayout( self );
	
	// size & position list
	self.List:SetSize( self:GetWide(), self:GetTall() - 16 );
	self.List:SetPos( 0, 0 );
	
	// resize the filename column
	self.ListFilename:ResizeColumn( self:GetWide() );
	
	// size & position add button
	self.AddButton:SetSize( 16, 16 );
	self.AddButton:SetPos( 0, self:GetTall() - 16 );
	
	// size & position remove button
	self.RemoveButton:SetSize( 16, 16 );
	self.RemoveButton:SetPos( 16, self:GetTall() - 16 );
	
	// size & position manage button
	self.ManageButton:SetSize( 25, 16 );
	self.ManageButton:SetPos( self:GetWide() - 25, self:GetTall() - 16 );
	
end


/* CUSTOM PANEL FUNCTIONS */


/*------------------------------------
	playlist container resized
------------------------------------*/
function PANEL:OnResized( )

	// let the playlist resize
	self.List.OnResized( self.List );

end

/*------------------------------------
	add a song to the playlist
------------------------------------*/
function PANEL:AddSong( filename )

	// make sure its not the "no song" item
	if ( filename == "__nomedia__" ) then
	
		// wtf you fucker, why are you doing this?
		return;
		
	end
	
	// get the current song items
	local curlines = self.List:GetLines();
	
	// loop through each item
	for _, line in pairs( curlines ) do
	
		// make sure the file isn't already in there
		if ( line.Filename == filename ) then
		
			// denied!
			// FIXME: do they want duplicate songs?
			return;
			
		end
	
	end
	
	// get the song
	local title = MuseX.ExtractFilename( filename );
	
	// create the new item
	local item = self.List:AddLine( title );
	
	// save the title
	item.Title = title;
	
	// save the filename
	item.Filename = filename;
	
	// update the "now playing" list
	MuseX.UpdateNowPlaying();

end


/*------------------------------------
	remove a song from playlist
------------------------------------*/
function PANEL:RemoveSong( filename )

	// get the current song items
	local curlines = self.List:GetLines();
	
	// loop through the items
	for _, line in pairs( curlines ) do
	
		// match the filename
		if ( line.Filename == filename ) then
		
			// remove that fucker
			self.List:RemoveLine( line:GetID() );
			
			// update the "now playing" list
			MuseX.UpdateNowPlaying();
			
			// leave, cuz we're done?
			return;
			
		end
	
	end
	
	// wasn't found... hahah!

end


/*------------------------------------
	clear the playlist
------------------------------------*/
function PANEL:ClearPlaylist( )

	// clear the list
	self.List:Clear();
	
	// update the "now playing" list
	MuseX.UpdateNowPlaying();

end


/*------------------------------------
	get the current playlist
------------------------------------*/
function PANEL:GetPlaylist( )

	// empty return table;
	local ret = {};
	
	// get all the items
	for k, v in pairs( self.List.Sorted ) do
	
		// add the filename to the table
		ret[ #ret + 1 ] = v.Filename;
		
	end

	// return the results
	return ret;

end


/*------------------------------------
	playlist paint
------------------------------------*/
function PANEL:Paint( )

	// nothing
	return true;
	
end


/*------------------------------------
	open the playlist file dialog
------------------------------------*/
function PANEL:OpenFileDialog( )

	// check if the file dialog has been opened
	if ( !self.FileDialog || !self.FileDialog:IsValid() ) then
	
		// if not, create it
		self.FileDialog = vgui.Create( "MXFileDialog" );
		
		// not visible
		self.FileDialog:SetVisible( false );
		
		// set the size
		local width = 500;
		local height = 400;
		self.FileDialog:SetSize( width, height );
		
		// start position
		local x = MuseX.GetSetting( "filedialogx" ) || ( ( ScrW() * 0.5 ) - ( width * 0.5 ) );
		local y = MuseX.GetSetting( "filedialogy" ) || ( ( ScrH() * 0.5 ) - ( height * 0.5 ) );
		
		// set position
		self.FileDialog:SetPos( x, y );
		
	end
	
	// open
	self.FileDialog:Open();

end


/*------------------------------------
	open the manage playlists dialog
------------------------------------*/
function PANEL:OpenManageDialog( )

	// check if the manager has been opened
	if ( !self.PlaylistManager || !self.PlaylistManager:IsValid() ) then
	
		// if not, create it
		self.PlaylistManager = vgui.Create( "MXPlaylistManager" );
		
		// not visible
		self.PlaylistManager:SetVisible( false );
		
		// set the size
		local width = 245;
		local height = 300;
		self.PlaylistManager:SetSize( width, height );
		
		// start position
		local x = MuseX.GetSetting( "managerx" ) || ( ( ScrW() * 0.5 ) - ( width * 0.5 ) );
		local y = MuseX.GetSetting( "managery" ) || ( ( ScrH() * 0.5 ) - ( height * 0.5 ) );
		
		// set position
		self.PlaylistManager:SetPos( x, y );
		
	end
	
	// open
	self.PlaylistManager:Open();
	
end


/*------------------------------------
	get a total count of songs
------------------------------------*/
function PANEL:GetPlaylistCount( )

	// count songs and return
	return ( #self.List.Lines );

end


/*------------------------------------
	gets a songs position in list
------------------------------------*/
function PANEL:GetPlaylistPosition( filename )

	// make sure the filename isnt nil
	if ( filename == nil ) then
	
		// return zero (means we dont have a position yet)
		return 0;
		
	end

	// get all the items
	for k, v in pairs( self.List.Sorted ) do
	
		// check if filename matches
		if ( filename == v.Filename ) then
		
			// return position
			return k;
		
		end
		
	end

	// not on list anymore!
	return 0;
	
end


/*------------------------------------
	get the song at position
------------------------------------*/
function PANEL:GetPlaylistSong( pos )

	// make sure the position exists
	if ( self.List.Sorted[ pos ] ) then
	
		// return it
		return self.List.Sorted[ pos ].Filename;
		
	end

end


/*------------------------------------
	get the song that is currently
	highlighted
------------------------------------*/
function PANEL:GetSelectedPlaylistSong( )

	// get selected items
	local selected = self.List:GetSelected();
	
	// make sure there is something selected
	if ( #selected > 0 ) then
	
		// return the first song
		return selected[ 1 ].Filename;
		
	end

end

// define
derma.DefineControl( "MXPlaylist", "", PANEL, "DPanel" );
